﻿using GDGeek;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class CabinetManager : MonoBehaviour
    {

        class Data
        {
            int position;
            int state;
            int type;
        }
        private Dictionary<byte, List<Data>> map_ = new Dictionary<byte, List<Data>>();
        [SerializeField]
        private CabinetSetup _setup;
        private int fixed_ = 0;
        public int fixedCabinet
        {
            get {
                return fixed_;
            }

        }
        public void setup() {

            Debug.LogError("d");
            TaskManager.Run(setup(CabinetGroup.Instance.count, CabinetGroup.Instance.fix));
        }
        void onChange(){
            for (int i = 0; i < _setup.count; ++i)
            {
                var item = CabinetGroup.Instance.getItem(i);
                float len = item.getLength();// CabinetGroup.Instance.getLength(item);
                Cabinet cabinet = _setup.getCabinet(i);
                var original = cabinet.original;
              
                Tween tween = TweenLocalPosition.Begin(cabinet.gameObject, 0.3f, original.position - new Vector3(0.05f * len, 0, 0));
                tween.method = Easing.Method.EaseInOutCirc;
                
               
            }
        }
        void Start()
        {
            if (CabinetGroup.Instance.initialized) {
                Task task = setup(CabinetGroup.Instance.count, CabinetGroup.Instance.fix);
                TaskManager.PushBack(task, delegate
                {
                    onChange();
                });
                TaskManager.Run(task);
            }
            CabinetGroup.Instance.onSetup += setup;
            CabinetGroup.Instance.onChange += onChange;

        }
        void OnDestroy()
        {
           
            if (CabinetGroup.IsInitialized)
            {
                CabinetGroup.Instance.onSetup -= setup;
                CabinetGroup.Instance.onChange -= onChange;
            }
          
        }


        public Task setup(int count, int _fixed)
        {
            Task task = _setup.setup(count);
            TaskManager.PushFront(task, delegate
            {

                fixed_ = _fixed;
            });
            return task;
        }

      


    }
}